import { type App } from 'vue';
import Crud from './index.vue'
import request from '/@/utils/request'

// 安装组件，app.use(Crud)
Crud.install = (app: App<Element>) =>{
  // 注册组件
  app.component('m-crud',Crud)
}
/**
 * 请求封装
 * @param api 
 * @param params 
 * @returns 
 */
export const postData = (api:any,params: any): Promise<any> => {
  let apiFun: ((param: any)=>Promise<any>) | undefined
  return new Promise((resolve, reject)=>{
    if(api && typeof api == 'function') {
      apiFun = api
      console.log('function=======');
      
    } else if(api && typeof api == 'string') {
      apiFun = (param: any) => request.post(api as any, param)
    }
    if(apiFun) {
      apiFun({
        ...params
      }).then((res: any)=>{
        resolve(res)
      }).catch(reject)
    } else {
      resolve({})
    }
  })
}
export default Crud